package cn.lingyangwl.agile.auth.service;

import cn.lingyangwl.agile.auth.*;
import cn.lingyangwl.agile.auth.model.entity.*;
import cn.lingyangwl.agile.auth.model.rqrs.*;
import cn.lingyangwl.agile.auth.model.rqrs.ldp.*;
import cn.lingyangwl.agile.auth.model.rqrs.third.*;
import cn.lingyangwl.agile.model.module.auth.*;
import cn.lingyangwl.framework.validation.*;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shenguangyang
 */
@Primary
@Service
public class AuthApiImpl implements AuthApi {
    @Resource
    private LocalUserService localUserService;
    @Resource
    private CaptchaService captchaService;
    @Resource
    private ThirdUserService thirdUserService;
    @Resource
    private OAuth2ClientService oauth2ClientService;
    @Resource
    private OAuth2TokenService tokenService;
    @Resource
    private AuthLoginLogService loginLogService;
    @Resource
    private ExtLdpConnectService ldpConnectService;
    @Resource
    private OAuth2ClientService clientService;
    @Resource
    private QrcodeLoginService qrcodeLoginService;

    @Override
    public List<ExtLdpConnectResp> listLdpConnectByClientId(String clientId) {
        return ldpConnectService.listConnectByClientId(clientId);
    }

    @Override
    public Integer deleteLoginLog(String endTime) {
        return loginLogService.deleteLogTask(endTime);
    }

    @Override
    public LoginUser getLoginUser() {
        return tokenService.getLoginUser();
    }

    @Override
    public void updateLoginUser(LoginUser loginUser) {
        this.tokenService.updateLoginUser(loginUser);
    }

    @Override
    public boolean removeAccessTokenByUserId(Long userId) {
        return tokenService.removeAccessTokenByUserId(userId);
    }

    @Override
    public boolean removeAccessTokenByUserIds(List<Long> userIds) {
        userIds.forEach(tokenService::removeAccessTokenByUserId);
        return true;
    }

    @Override
    public void deleteCaptcha(String uuid) {
        captchaService.deleteCaptcha(uuid);
    }

    @Override
    public boolean checkCaptcha(CaptchaCheckReq req) {
        return captchaService.checkCaptcha(req.getImageCaptcha(), req.getUuid());
    }

    @Override
    public OAuth2Client getByClientId(String clientId) {
        return oauth2ClientService.getByClientId(clientId);
    }

    @Override
    public LocalUser getUser(LocalUserGetReq req) {
        return localUserService.getUser(req);
    }

    @Override
    public LocalUser getUserByUserId(Long userId) {
        return localUserService.getUserByUserId(userId);
    }

    @Override
    public void updateLocalUser(LocalUser req) {
        localUserService.updateLocalUser(req);
    }

    @Override
    public void deleteLocalUser(Long userId) {
        localUserService.deleteLocalUser(userId);
    }

    @Override
    public void batchDeleteLocalUser(String tenantId, List<Long> userIds) {
        localUserService.batchDeleteLocalUser(tenantId, userIds);
    }

    @Override
    public Long saveLocalUser(LocalUser req) {
        ValidationUtils.validateFast(req);
        localUserService.saveLocalUser(req);
        return req.getAuthId();
    }

    @Override
    public void batchUpdateLocalUser(List<LocalUser> reqList) {
        localUserService.batchUpdateLocalUser(reqList);
    }

    @Override
    public void cancelAccount(CancelAccountReq req) {
        localUserService.cancelAccount(req);
    }

    @Override
    public List<LocalUser> listAuth(LocalUserListReq query) {
        return localUserService.listAuth(query);
    }

    @Override
    public void saveThirdAuth(ThirdUser req) {
        thirdUserService.save(req);
    }

    @Override
    public void refreshClientAuthorityByRoleIds(List<Long> roleIds, boolean roleDeleted) {
        clientService.refreshClientAuthorityByRoleIds(roleIds, roleDeleted);
    }

    @Override
    public void userScanQrcode(LoginQrcodeInfo req) {
        qrcodeLoginService.userScanQrcode(req);
    }
}
